package com.qf.fmall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.fmall.product.entity.ProductComments;
import com.qf.fmall.product.mapper.ProductCommentsMapper;
import com.qf.fmall.product.service.IProductCommentsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.fmall.user.entity.User;
import com.qf.fmall.user.mapper.UserDao;
import com.qf.fmall.vo.PageVo;
import com.qf.fmall.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author weijy
 * @since 2024-01-16
 */
@Service
@Slf4j
public class ProductCommentsServiceImpl extends ServiceImpl<ProductCommentsMapper, ProductComments> implements IProductCommentsService {

    @Autowired
    private ProductCommentsMapper productCommentsMapper;
    @Autowired
    private UserDao userDao;

    @Override
    public PageVo getCommentsById(Integer productId, Integer pageNum, Integer limit) {

        //1. 创建分页对象
        Page<ProductComments> page = new Page<>(pageNum, limit);

        //2. 创建查询条件对象
        QueryWrapper<ProductComments> queryWrapper = new QueryWrapper<ProductComments>().eq("product_id", productId);

        page(page, queryWrapper);
        List<ProductComments> records = page.getRecords();
        addUserData(records);

        //3. 获取分页信息
        PageVo pageVo = new PageVo();
        pageVo.setCount(page.getTotal());
        pageVo.setList(records);
        pageVo.setPageCount(page.getPages());

        return pageVo;
    }

    private void addUserData(List<ProductComments> records) {
        for (ProductComments record : records) {
            String userId = record.getUserId();
            User user = userDao.selectById(userId);
            record.setUsername(user.getUsername());
            record.setNickname(user.getNickname());
            record.setUserImg(user.getUserImg());
        }
    }

    @Override
    public ResultVo getCommentsCount(Integer productId) {


        HashMap<String, Object> hashMap = new HashMap<>();

        long good = count(new QueryWrapper<ProductComments>().eq("comm_type", 1));
        long mid = count(new QueryWrapper<ProductComments>().eq("comm_type", 0));
        long bad = count(new QueryWrapper<ProductComments>().eq("comm_type", -1));
        long total = good + mid + bad;

        String percent = new BigDecimal(good + "")
                .multiply(new BigDecimal("100"))
                .divide(new BigDecimal(total + ""), 2, RoundingMode.HALF_UP)
                .toString();

        hashMap.put("goodTotal",good);
        hashMap.put("total",total);
        hashMap.put("badTotal",bad);
        hashMap.put("midTotal",mid);
        hashMap.put("percent",percent);


        return new ResultVo(10000,"success",hashMap);
    }
}
